package com.example.coursehub.coursemanagement.controller;

import com.example.coursehub.coursemanagement.entity.Course;
import com.example.coursehub.coursemanagement.entity.Chapter;
import com.example.coursehub.coursemanagement.entity.Material;
import com.example.coursehub.coursemanagement.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 课程管理控制器
 */
@RestController
@RequestMapping("/api/courses")
@CrossOrigin(origins = "*")
public class CourseController {
    
    @Autowired
    private CourseService courseService;
    
    // 课程相关接口
    @PostMapping
    public ResponseEntity<Course> createCourse(@RequestBody Course course) {
        Course createdCourse = courseService.createCourse(course);
        return ResponseEntity.ok(createdCourse);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<Course> updateCourse(@PathVariable Long id, @RequestBody Course course) {
        try {
            Course updatedCourse = courseService.updateCourse(id, course);
            return ResponseEntity.ok(updatedCourse);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCourse(@PathVariable Long id) {
        try {
            courseService.deleteCourse(id);
            return ResponseEntity.ok().build();
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Course> getCourseById(@PathVariable Long id) {
        Optional<Course> course = courseService.getCourseById(id);
        return course.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping
    public ResponseEntity<List<Course>> getAllCourses() {
        List<Course> courses = courseService.getAllCourses();
        return ResponseEntity.ok(courses);
    }
    
    @GetMapping("/teacher/{teacherId}")
    public ResponseEntity<List<Course>> getCoursesByTeacher(@PathVariable Long teacherId) {
        List<Course> courses = courseService.getCoursesByTeacher(teacherId);
        return ResponseEntity.ok(courses);
    }
    
    @GetMapping("/search")
    public ResponseEntity<List<Course>> searchCourses(@RequestParam String name) {
        List<Course> courses = courseService.searchCoursesByName(name);
        return ResponseEntity.ok(courses);
    }
    
    // 章节相关接口
    @PostMapping("/{courseId}/chapters")
    public ResponseEntity<Chapter> createChapter(@PathVariable Long courseId, @RequestBody Chapter chapter) {
        chapter.setCourseId(courseId);
        Chapter createdChapter = courseService.createChapter(chapter);
        return ResponseEntity.ok(createdChapter);
    }
    
    @PutMapping("/chapters/{id}")
    public ResponseEntity<Chapter> updateChapter(@PathVariable Long id, @RequestBody Chapter chapter) {
        try {
            Chapter updatedChapter = courseService.updateChapter(id, chapter);
            return ResponseEntity.ok(updatedChapter);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/chapters/{id}")
    public ResponseEntity<Void> deleteChapter(@PathVariable Long id) {
        try {
            courseService.deleteChapter(id);
            return ResponseEntity.ok().build();
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @GetMapping("/chapters/{id}")
    public ResponseEntity<Chapter> getChapterById(@PathVariable Long id) {
        Optional<Chapter> chapter = courseService.getChapterById(id);
        return chapter.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/{courseId}/chapters")
    public ResponseEntity<List<Chapter>> getChaptersByCourse(@PathVariable Long courseId) {
        List<Chapter> chapters = courseService.getChaptersByCourse(courseId);
        return ResponseEntity.ok(chapters);
    }
    
    // 资料相关接口
    @PostMapping("/{courseId}/materials")
    public ResponseEntity<Material> createMaterial(@PathVariable Long courseId, @RequestBody Material material) {
        material.setCourseId(courseId);
        Material createdMaterial = courseService.createMaterial(material);
        return ResponseEntity.ok(createdMaterial);
    }
    
    @PutMapping("/materials/{id}")
    public ResponseEntity<Material> updateMaterial(@PathVariable Long id, @RequestBody Material material) {
        try {
            Material updatedMaterial = courseService.updateMaterial(id, material);
            return ResponseEntity.ok(updatedMaterial);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/materials/{id}")
    public ResponseEntity<Void> deleteMaterial(@PathVariable Long id) {
        try {
            courseService.deleteMaterial(id);
            return ResponseEntity.ok().build();
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @GetMapping("/materials/{id}")
    public ResponseEntity<Material> getMaterialById(@PathVariable Long id) {
        Optional<Material> material = courseService.getMaterialById(id);
        return material.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/{courseId}/materials")
    public ResponseEntity<List<Material>> getMaterialsByCourse(@PathVariable Long courseId) {
        List<Material> materials = courseService.getMaterialsByCourse(courseId);
        return ResponseEntity.ok(materials);
    }
    
    @GetMapping("/chapters/{chapterId}/materials")
    public ResponseEntity<List<Material>> getMaterialsByChapter(@PathVariable Long chapterId) {
        List<Material> materials = courseService.getMaterialsByChapter(chapterId);
        return ResponseEntity.ok(materials);
    }
}